﻿package view.appDrag
{
    import com.sh.game.it.plugin.*;
    import flash.display.*;
    import flash.events.*;

    public class AppDragMgr extends Object
    {
        private static var mInstance:AppDragManager;
        private static var eventDispatch:EventDispatcher;
        public static const CLINGY:int = 1;
        public static const DRAG:int = 2;

        public function AppDragMgr()
        {
            return;
        }// end function

        public static function clingyItem(param1:DisplayObject, param2:Object = null, param3:DisplayObject = null, param4:int = 1, param5:String = "bag") : void
        {
            instance.clingyItem(param1, param2, param3, param4, param5);
            return;
        }// end function

        public static function addEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            edispatch.addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public static function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            edispatch.removeEventListener(param1, param2, param3);
            return;
        }// end function

        public static function RegisterModule(param1:IRoot) : void
        {
            instance.RegisterModule(param1);
            return;
        }// end function

        public static function dispatchEvent(event:AppDragEvent) : void
        {
            edispatch.dispatchEvent(event);
            return;
        }// end function

        public static function cencelDrag() : void
        {
            instance.cencelDrag();
            return;
        }// end function

        private static function get instance() : AppDragManager
        {
            if (!mInstance)
            {
                mInstance = new AppDragManager();
            }
            return mInstance;
        }// end function

        private static function get edispatch() : EventDispatcher
        {
            if (!eventDispatch)
            {
                eventDispatch = new EventDispatcher();
            }
            return eventDispatch;
        }// end function

    }
}

class AppDragManager extends Object
{
    private var mDragIn:Boolean = false;
    private var mClingyDisplay:DisplayObject;
    private var mItemData:Object = null;
    private var mKey:String = null;
    private var mDrawClingy:Sprite = null;
    private var mDelayID:int = 0;
    private var mStage:Stage = null;
    private var _pluginVec:Vector.<IRoot>;

    function AppDragManager()
    {
        return;
    }// end function

    public function clingyItem(param1:DisplayObject, param2:Object = null, param3:DisplayObject = null, param4:int = 1, param5:String = "bag") : void
    {
        var _loc_6:DisplayObject = null;
        if (this.mDragIn)
        {
            return;
        }
        if (param1)
        {
            if (param1.stage == null)
            {
                return;
            }
            if (this.mClingyDisplay == param1)
            {
                return;
            }
            this.mDragIn = true;
            this.mStage = param1.stage;
            this.mClingyDisplay = param1;
            this.mItemData = param2;
            this.mKey = param5;
            _loc_6 = null;
            this.drawClingy(param3);
            if (param4 == 1)
            {
                this.invlaidateListMouseClingy();
            }
            else
            {
                this.invalidateMouseDrag();
            }
        }
        return;
    }// end function

    private function invalidateMouseDrag() : void
    {
        var func:* = function () : void
        {
            if (mDelayID > 0)
            {
                clearTimeout(mDelayID);
                mDelayID = 0;
            }
            appMain.addEventListener(MouseEvent.MOUSE_UP, enterDragHandler, false, 0, true);
            return;
        }// end function
        ;
        if (this.mDelayID > 0)
        {
            clearTimeout(this.mDelayID);
            this.mDelayID = 0;
        }
        this.mDelayID = setTimeout(func, 1000 / 24);
        return;
    }// end function

    private function invlaidateListMouseClingy() : void
    {
        var func:* = function () : void
        {
            if (mDelayID > 0)
            {
                clearTimeout(mDelayID);
                mDelayID = 0;
            }
            appMain.addEventListener(MouseEvent.CLICK, enterClingyHandler, false, 0, true);
            return;
        }// end function
        ;
        if (this.mDelayID > 0)
        {
            clearTimeout(this.mDelayID);
            this.mDelayID = 0;
        }
        this.mDelayID = setTimeout(func, 1000 / 24);
        return;
    }// end function

    private function drawClingy(param1:DisplayObject) : void
    {
        var _loc_2:BitmapData = null;
        var _loc_3:Bitmap = null;
        if (!this.mDrawClingy)
        {
            this.mDrawClingy = new Sprite();
        }
        if (param1)
        {
            this.mDrawClingy.addChild(param1);
            this.mDrawClingy.x = this.appMain.mouseX - param1.width / 2;
            this.mDrawClingy.y = this.appMain.mouseY - param1.height / 2;
            this.mDrawClingy.mouseEnabled = false;
            this.mDrawClingy.mouseChildren = false;
        }
        else
        {
            _loc_2 = new BitmapData(this.mClingyDisplay.width, this.mClingyDisplay.height, true, 0);
            _loc_2.draw(this.mClingyDisplay);
            _loc_3 = new Bitmap(_loc_2);
            this.mDrawClingy.addChild(_loc_3);
            this.mDrawClingy.x = this.appMain.mouseX - _loc_3.width / 2;
            this.mDrawClingy.y = this.appMain.mouseY - _loc_3.height / 2;
        }
        this.mDrawClingy.startDrag();
        this.appMain.addChild(this.mDrawClingy);
        return;
    }// end function

    private function enterClingyHandler(event:MouseEvent) : void
    {
        var _loc_3:DisplayObject = null;
        var _loc_2:* = new AppDragEvent(AppDragEvent.CLINGY, this.mClingyDisplay, this.mItemData, new Point(event.stageX, event.stageY));
        AppDragMgr.dispatchEvent(_loc_2);
        if (this.mDrawClingy)
        {
            while (this.mDrawClingy.numChildren > 0)
            {
                
                _loc_3 = this.mDrawClingy.removeChildAt(0);
                if (_loc_3 is Bitmap)
                {
                    Bitmap(_loc_3).bitmapData.dispose();
                    Bitmap(_loc_3).bitmapData = null;
                }
                _loc_3 = null;
            }
            this.appMain.removeChild(this.mDrawClingy);
            this.mDrawClingy = null;
        }
        this.appMain.removeEventListener(MouseEvent.CLICK, this.enterClingyHandler);
        this.mClingyDisplay = null;
        this.mItemData = null;
        this.mDragIn = false;
        return;
    }// end function

    public function cencelDrag() : void
    {
        var _loc_1:DisplayObject = null;
        if (this.mDrawClingy)
        {
            while (this.mDrawClingy.numChildren > 0)
            {
                
                _loc_1 = this.mDrawClingy.removeChildAt(0);
                if (_loc_1 is Bitmap)
                {
                    Bitmap(_loc_1).bitmapData.dispose();
                    Bitmap(_loc_1).bitmapData = null;
                }
                _loc_1 = null;
            }
            this.appMain.removeChild(this.mDrawClingy);
            this.mDrawClingy = null;
        }
        return;
    }// end function

    private function enterDragHandler(event:MouseEvent) : void
    {
        var _loc_6:int = 0;
        var _loc_7:DisplayObject = null;
        var _loc_8:int = 0;
        var _loc_9:IRoot = null;
        var _loc_10:IRoot = null;
        var _loc_11:IRoot = null;
        var _loc_2:* = new Point(event.stageX, event.stageY);
        var _loc_3:* = new AppDragEvent(AppDragEvent.DRAG, this.mClingyDisplay, this.mItemData, _loc_2);
        if (this.mDrawClingy)
        {
            while (this.mDrawClingy.numChildren > 0)
            {
                
                _loc_7 = this.mDrawClingy.removeChildAt(0);
                if (_loc_7 is Bitmap)
                {
                    Bitmap(_loc_7).bitmapData.dispose();
                    Bitmap(_loc_7).bitmapData = null;
                }
                _loc_7 = null;
            }
            this.appMain.removeChild(this.mDrawClingy);
            this.mDrawClingy = null;
        }
        var _loc_4:* = new Vector.<IRoot>;
        var _loc_5:Point = null;
        if (this._pluginVec)
        {
            _loc_6 = 0;
            while (_loc_6 < this._pluginVec.length)
            {
                
                _loc_10 = this._pluginVec[_loc_6];
                if ((_loc_10 as DisplayObjectContainer).parent)
                {
                    if ((_loc_10 as DisplayObjectContainer).parent.visible)
                    {
                        _loc_5 = (_loc_10 as Object).globalToLocal(_loc_2);
                        if (_loc_5.x > 0 && _loc_5.y > 0 && _loc_5.x < (_loc_10 as Object).width && _loc_5.y < (_loc_10 as Object).height)
                        {
                            _loc_4.push(_loc_10);
                        }
                    }
                }
                else if ((_loc_10 as Object).visible)
                {
                    _loc_5 = (_loc_10 as Object).globalToLocal(_loc_2);
                    if (_loc_5.x > 0 && _loc_5.y > 0 && _loc_5.x < (_loc_10 as Object).width && _loc_5.y < (_loc_10 as Object).height)
                    {
                        _loc_4.push(_loc_10);
                    }
                }
                _loc_6++;
            }
            _loc_8 = -1;
            _loc_9 = null;
            _loc_6 = 0;
            while (_loc_6 < _loc_4.length)
            {
                
                _loc_11 = _loc_4[_loc_6];
                if (_loc_8 < (_loc_11 as Object).parent.getChildIndex(_loc_11 as Object))
                {
                    _loc_8 = (_loc_11 as Object).parent.getChildIndex(_loc_11 as Object);
                    _loc_9 = _loc_11;
                }
                _loc_6++;
            }
            if (_loc_9)
            {
                (_loc_9 as IRoot).DragOver(this.mItemData, _loc_2, this.mKey);
                this.mItemData = null;
                this.appMain.removeEventListener(MouseEvent.MOUSE_UP, this.enterDragHandler);
                this.mClingyDisplay = null;
                this.mItemData = null;
                this.mDragIn = false;
                return;
            }
        }
        this.appMain.removeEventListener(MouseEvent.MOUSE_UP, this.enterDragHandler);
        this.mClingyDisplay = null;
        this.mItemData = null;
        this.mDragIn = false;
        AppDragMgr.dispatchEvent(_loc_3);
        return;
    }// end function

    private function get appMain() : DisplayObjectContainer
    {
        return this.mStage;
    }// end function

    public function RegisterModule(param1:IRoot) : void
    {
        if (this._pluginVec == null)
        {
            this._pluginVec = new Vector.<IRoot>;
        }
        this._pluginVec.push(param1);
        return;
    }// end function

}

